package com.everflourish.yeah100.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.everflourish.yeah100.R;
import com.everflourish.yeah100.entity.statistics.ObjectiveTopic;
import com.everflourish.yeah100.ui.MyHScrollView;
import com.everflourish.yeah100.utils.StringUtil;

import java.text.DecimalFormat;
import java.util.List;

/**
 * 作者：Created by liqisi on 2016/7/7.
 * 时间：2016/7/7
 */
public class FragmentTeacherErrorAdapter extends RecyclerView.Adapter<FragmentTeacherErrorAdapter.TeacherErrorViewHolder> {

    private LayoutInflater mInflater;
    private List<ObjectiveTopic> mList;
    private Context mContext;
    private List<MyHScrollView> myHScrollViews;

    public FragmentTeacherErrorAdapter(Context mContext, List<MyHScrollView> myHScrollViews,
                                       List<ObjectiveTopic> mList) {
        this.mList = mList;
        this.mContext = mContext;
        this.myHScrollViews = myHScrollViews;
        mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public TeacherErrorViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_statistics_teacher_error, parent, false);
        TeacherErrorViewHolder viewHolder = new TeacherErrorViewHolder(view);
        myHScrollViews.add(viewHolder.myHScrollView);
        viewHolder.myHScrollView.setMyHScrollViews(myHScrollViews);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(TeacherErrorViewHolder holder, int position) {
        ObjectiveTopic objectiveTopic = mList.get(position);
        holder.mQuestionNo.setText(objectiveTopic.getSequenceNo());
        String standardAnswer = getStandardAnswer(objectiveTopic
                .getStandardAnswer());
        holder.mStandardAnswer.setText(standardAnswer);
        DecimalFormat df = new DecimalFormat("#0.0");
        if ((objectiveTopic.getFailureRate() * 100f) == 0.0) {
            holder.mErrorRate.setText(0.0 + "%");
        } else {
            holder.mErrorRate.setText(df.format(objectiveTopic
                    .getFailureRate() * 100f) + "%");
        }
        holder.mSelectA.setText(objectiveTopic.getSubTotalOption1() + "");
        holder.mSelectB.setText(objectiveTopic.getSubTotalOption2() + "");
        holder.mSelectC.setText(objectiveTopic.getSubTotalOption3() + "");
        holder.mSelectD.setText(objectiveTopic.getSubTotalOption4() + "");
        if ((position + 1) % 2 == 0) {  // 双
            holder.mItemLl.setBackgroundColor(mContext.getResources()
                    .getColor(R.color.e_student_item_single));
        } else { // 单
            holder.mItemLl.setBackgroundColor(mContext.getResources()
                    .getColor(R.color.e_student_item_double));
        }
    }

    /**
     * 将标准答案(10进制)转换为可视化答案（如1 = A）
     *
     * @param sNumber
     * @return
     */
    private String getStandardAnswer(String sNumber) {
        int number = StringUtil.parseInt(sNumber, -1);
        if (number == -1) {
            return "";
        }
        String binaryString = Integer.toBinaryString(number);
        char[] binarys = binaryString.toCharArray();
        StringBuffer sbuf = new StringBuffer();
        char[] reverseBinarys = setReverse(binarys);
        for (int j = 0; j < reverseBinarys.length; j++) {
            int binary = Integer.parseInt(reverseBinarys[j] + "");
            switch (j) {
                case 0:
                    setStandardAnswer(sbuf, binary, "A");
                    break;
                case 1:
                    setStandardAnswer(sbuf, binary, "B");
                    break;
                case 2:
                    setStandardAnswer(sbuf, binary, "C");
                    break;
                case 3:
                    setStandardAnswer(sbuf, binary, "D");
                    break;
                case 4:
                    setStandardAnswer(sbuf, binary, "E");
                    break;
                case 5:
                    setStandardAnswer(sbuf, binary, "F");
                    break;
                case 6:
                    setStandardAnswer(sbuf, binary, "G");
                    break;
                case 7:
                    setStandardAnswer(sbuf, binary, "H");
                    break;
            }
        }
        return sbuf.toString();
    }

    private char[] setReverse(char[] binarys) {
        char[] reverseBinarys = new char[binarys.length];
        for (int i = binarys.length - 1, j = 0; i >= 0; i--, j++) {
            reverseBinarys[j] = binarys[i];
        }
        return reverseBinarys;
    }

    private void setStandardAnswer(StringBuffer sbuf, int binary, String answer) {
        if (binary == 1) {
            sbuf.append(answer);
        }
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public class TeacherErrorViewHolder extends RecyclerView.ViewHolder {

        private TextView mQuestionNo;
        private TextView mStandardAnswer;
        private TextView mErrorRate;
        private TextView mSelectA;
        private TextView mSelectB;
        private TextView mSelectC;
        private TextView mSelectD;
        private MyHScrollView myHScrollView;
        private LinearLayout mItemLl;

        public TeacherErrorViewHolder(View itemView) {
            super(itemView);
            mQuestionNo = (TextView) itemView.findViewById(R.id.item_t_question_no);
            mStandardAnswer = (TextView) itemView.findViewById(R.id.item_t_standard_answer);
            mErrorRate = (TextView) itemView.findViewById(R.id.item_t_error_rate);
            mSelectA = (TextView) itemView.findViewById(R.id.item_t_select_a);
            mSelectB = (TextView) itemView.findViewById(R.id.item_t_select_b);
            mSelectC = (TextView) itemView.findViewById(R.id.item_t_select_c);
            mSelectD = (TextView) itemView.findViewById(R.id.item_t_select_d);
            myHScrollView = (MyHScrollView) itemView.findViewById(R.id.item_t_error_title_mhv);
            mItemLl = (LinearLayout) itemView.findViewById(R.id.teacher_error_ll);
        }
    }
}